const scheduleForm = document.getElementById('scheduleForm');
const scheduleList = document.getElementById('scheduleList');
const dateInput = document.getElementById('date');
const queryButton = document.getElementById('queryButton');

// 添加日程
scheduleForm.addEventListener('submit', async (e) => {
    e.preventDefault();
    const date = dateInput.value;
    const time = document.getElementById('time').value;
    const activity = document.getElementById('activity').value;

    try {
        const response = await fetch('/add', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ date, time, activity })
        });
        const data = await response.json();
        if (data.success) {
            alert('日程添加成功');
            scheduleForm.reset();
            displaySchedules(date);
        } else {
            alert('日程添加失败');
        }
    } catch (error) {
        console.error('请求出错:', error);
    }
});

// 查询日程
queryButton.addEventListener('click', async () => {
    const date = dateInput.value;
    displaySchedules(date);
});

// 显示日程
async function displaySchedules(date) {
    scheduleList.innerHTML = '';
    try {
        const response = await fetch(`/query?date=${date}`);
        const data = await response.json();
        data.schedules.forEach(schedule => {
            const listItem = document.createElement('li');
            listItem.classList.add('p-4', 'flex', 'items-center', 'justify-between', 'hover:bg-gray-100', 'transition', 'duration-300');
            listItem.innerHTML = `
                <span class="text-gray-700">${schedule.time}: ${schedule.activity}</span>
                <button class="text-red-500 hover:text-red-700 focus:outline-none">
                    <i class="fa-solid fa-trash"></i> 删除
                </button>
            `;

            const deleteButton = listItem.querySelector('button');
            deleteButton.addEventListener('click', async () => {
                try {
                    const response = await fetch('/delete', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ date: schedule.date, time: schedule.time, activity: schedule.activity })
                    });
                    const result = await response.json();
                    if (result.success) {
                        listItem.remove();
                    } else {
                        alert('删除失败');
                    }
                } catch (error) {
                    console.error('删除请求出错:', error);
                }
            });

            scheduleList.appendChild(listItem);
        });
    } catch (error) {
        console.error('查询请求出错:', error);
    }
}

// 初始化显示今天的日程
const today = new Date().toISOString().split('T')[0];
dateInput.value = today;
displaySchedules(today);
